from slixmpp import ClientXMPP
from newsapi import NewsApiClient
import time

class Bot(ClientXMPP):
    def __init__(self, jid, password):
        ClientXMPP.__init__(self, jid, password)

        self.add_event_handler("session_start", self.start)
        self.add_event_handler("message", self.message)

        self.news_api = NewsApiClient(api_key='ваш_api_ключ')

    def start(self, event):
        self.send_presence()
        self.get_roster()

    def message(self, msg):
        if msg['type'] in ('chat', 'normal'):
            message_text = msg['body']

            if "news" in message_text.lower():
                news_titles = self.get_news_titles()

                for title in news_titles:
                    msg.reply(title).send()
                    time.sleep(1)

    def get_news_titles(self):
        headlines = self.news_api.get_top_headlines(language='ru', country='ru', page_size=5)
        return [article['title'] for article in headlines['articles']]

if __name__ == '__main__':
    
    xmpp = Bot("ваш_jabber_id", "ваш_пароль")
    xmpp.connect()
    xmpp.process()



